using MediatR;
using B.S.BasicData.Infrature;
using B.S.BasicData.Domain;
using B.S.BasicData.EroorCode;
using Microsoft.EntityFrameworkCore;

namespace B.S.BasicData.Api
{
    public class DeepProcessingJoinQueryHandler : IRequestHandler<DeepProcessingJoinQueryRequest, ApiResult<List<DeepProcessingJoinDto>>>
    {
        private readonly AppDbContext _context;

        public DeepProcessingJoinQueryHandler(AppDbContext context)
        {
            _context = context;
        }

        public async Task<ApiResult<List<DeepProcessingJoinDto>>> Handle(DeepProcessingJoinQueryRequest request, CancellationToken cancellationToken)
        {
            try
            {
                // 构建连表查询
                var query = from dpi in _context.DeepProcessingInfos
                           join dpei in _context.DeepProcessingEnterpriseInfos
                           on dpi.BatchNo equals dpei.BatchNo into dpeiGroup
                           from dpei in dpeiGroup.DefaultIfEmpty()
                           join si in _context.SupplierInfos
                           on dpi.BatchNo equals si.BatchNo into siGroup
                           from si in siGroup.DefaultIfEmpty()
                           join st in _context.SupplierTypes
                           on si.SupplierTypeId equals st.Id into stGroup
                           from st in stGroup.DefaultIfEmpty()
                           select new DeepProcessingJoinDto
                           {
                               // 深加工信息
                               DeepProcessingInfoId = dpi.Id,
                               TeamGroup = dpi.TeamGroup,
                               ResponsiblePerson = dpi.ResponsiblePerson,
                               DeepProcessingBatchNo = dpi.BatchNo,

                               // 深加工企业信息
                               DeepProcessingEnterpriseInfoId = dpei != null ? dpei.Id : 0,
                               CompanyName = dpei != null ? dpei.CompanyName : null,
                               ProductionLicenseNumber = dpei != null ? dpei.ProductionLicenseNumber : null,
                               EnterpriseBatchNo = dpei != null ? dpei.BatchNo : null,

                               // 供应商信息
                               SupplierInfoId = si != null ? si.Id : 0,
                               SupplierTypeId = si != null ? si.SupplierTypeId : 0,
                               SupplierBatchNo = si != null ? si.BatchNo : null,
                               SupplierTypeName = st != null ? st.TypeName : null,

                               // 公共字段
                               BatchNo = dpi.BatchNo
                           };

                // 应用生产批次筛选条件
                if (!string.IsNullOrWhiteSpace(request.BatchNo))
                {
                    query = query.Where(x => x.BatchNo.Contains(request.BatchNo));
                }

                // 执行查询
                var result = await query.ToListAsync(cancellationToken);

                return ApiResult<List<DeepProcessingJoinDto>>.Success(result, "查询成功");
            }
            catch (Exception ex)
            {
                return ApiResult<List<DeepProcessingJoinDto>>.Fail($"查询失败: {ex.Message}", ApiResultCode.Error);
            }
        }
    }
} 